package com.ctgu.ImageRecognition.controller;

import com.ctgu.ImageRecognition.entity.User;
import com.ctgu.ImageRecognition.service.UserService;
import com.ctgu.ImageRecognition.util.ResultUtil;
import com.ctgu.ImageRecognition.vo.ResultVO;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static org.springframework.util.ObjectUtils.isEmpty;

/**
 * (User)表控制层
 *
 * @author 
 * @since 2023-07-05 18:31:03
 */
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    @GetMapping("/test")
    public void test(String username){
        User user = userService.login(username);
//        System.out.println("user.getPassword() = " + user.getPassword());
        if(user == null) System.out.println("1 = " + 1);
    }

    @RequestMapping("/login")
    public ResultVO<User> login(@RequestBody User user){
        User resultUser = userService.login(user.getUsername());
        if(!resultUser.getPassword().equals(user.getPassword())){
            return ResultUtil.fail("登录失败",null);
        }
        return ResultUtil.success("成功登录",resultUser);

    }

    @RequestMapping("/register")
    public ResultVO register(@RequestBody User user){
        user.setUserCreated(new Date());
        int ResultNum = userService.register(user);
        if(ResultNum <0 ) return ResultUtil.fail("注册失败",null);

        return ResultUtil.success("注册成功",null);
    }

    /**
    * Description:查询出用户信息
    * @author: 吴杰
    * @date: 2023/7/19 12:00
    * @param:
     * * @return:
    */
    @RequestMapping("/queryUser")
    public ResultVO selectUser(@RequestParam String username){
        System.out.println("username = " + username);
        User user = userService.login(username);
        return ResultUtil.success("查询成功",user);
    }
    /**
    * Description:修改用户信息
    * @author: 吴杰
    * @date: 2023/7/19 13:42
    * @param:
     * * @return:
    */
    @RequestMapping("/update")
    public ResultVO update(@RequestBody User user) {
        int resultNum = userService.save(user);
        if (resultNum < 0) {
            return ResultUtil.fail("更新失败",null);
        }
        return ResultUtil.success("更新成功",null);
    }
}

